<!--历史数据-->
<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>

      <a-card
        :bordered="false"
        title="每日耗电量和刮粪次数统计"
        :body-style="{padding: '16px 6px 0 0'}">
        <v-chart
          ref="temperatureHumidityChart"
          style="height: 362px;"
          :option="temperatureHumidityChartOption"/>
      </a-card>

      <a-card
        :bordered="false"
        title="每日耗电量和温度统计"
        :body-style="{padding: '16px 6px 0 0'}">
        <v-chart
          ref="consumptionTemperatureChart"
          style="height: 362px;"
          :option="consumptionTemperatureChartOption"/>
      </a-card>

    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="dsstcId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space />
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>
</template>

<script>
import { provide } from 'vue';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LineChart, BarChart } from 'echarts/charts';
import {
  GridComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components';
import VChart, { THEME_KEY } from 'vue-echarts';
import 'echarts-wordcloud';
import { ChartTheme } from 'ele-admin-pro';
import moment from 'moment';
import {
  DeviceSlurryScraperTodayConsumptionApi
} from "@/api/property/slurryscraper/DeviceSlurryScraperTodayConsumptionApi";


use([
  CanvasRenderer,
  LineChart,
  BarChart,
  GridComponent,
  TooltipComponent,
  LegendComponent
]);
provide(THEME_KEY, ChartTheme);

export default {
  name: 'DeviceSlurryscraperDayCalenConsumption',
  components: {
    VChart
  },
  props: {
    data: Object
  },
  async mounted() {
    this.getTemperatureHumidityData();
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '时间',
          dataIndex: 'today'
        },
        {
          title: '当天用电总消耗量(kwh)',
          dataIndex: 'consumptionTotal',
          customRender: function (row) {
            return row.text ?  (row.text * 0.01).toFixed(2) : 0;
          }
        },
        {
          title: '当天刮粪次数',
          dataIndex: 'calenNum',
        },
        {
          title: '最低温度(℃)',
          dataIndex: 'minTemperature',
        },
        {
          title: '最高温度(℃)',
          dataIndex: 'maxTemperature',
        }
      ],
      // 表格搜索条件
      where: {"recordId" : this.data.recordId, 'sortBy' : 'desc'},
      temperatureHumidityData: [],
    }
  },
  computed: {
    // 最近1小时温度情况折线图配置
    temperatureHumidityChartOption() {
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        title: {
          text: '每天电消耗量和刮粪次数的统计',
          left: "center",
        },
        legend: {
          data: ['刮粪次数', '电消耗量(kwh)'],
          right: 20
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true
            },
            // prettier-ignore
            data: this.temperatureHumidityData.map(d => d.today)
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '刮粪次数',
            position: 'right',
            alignTicks: true,
            minInterval: 1,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#5470C6'
              }
            },
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '电消耗量(kwh)',
            position: 'left',
            minInterval: 0.1,
            alignTicks: true,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#EE6666'
              }
            },
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series: [
          {
            name: '刮粪次数',
            type: 'bar',
            data: this.temperatureHumidityData.map(d => d.calenNum),
            yAxisIndex: 0,
            barWidth : 15
          },
          {
            name: '电消耗量',
            type: 'line',
            data: this.temperatureHumidityData.map(d => d.consumptionTotal),
            yAxisIndex: 1,
          },
        ]
      }
    },
    consumptionTemperatureChartOption(){
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        title: {
          text: '每日耗电和温度的对比',
          left: "center",
        },
        legend: {
          data: ['每日耗电量', '最低温度(℃)'],
          right: 20
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true
            },
            // prettier-ignore
            data: this.temperatureHumidityData.map(d => d.today)
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '每日耗电量(kwh)',
            position: 'right',
            alignTicks: true,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#5470C6'
              }
            },
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '最低温度(℃)',
            position: 'left',
            alignTicks: true,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#EE6666'
              }
            },
            axisLabel: {
              formatter: '{value} °C'
            }
          }
        ],
        series: [
          {
            name: '每日耗电量',
            type: 'bar',
            data: this.temperatureHumidityData.map(d => d.consumptionTotal),
            yAxisIndex: 0,
            barWidth : 15
          },
          {
            name: '最低温度(℃)',
            type: 'line',
            data: this.temperatureHumidityData.map(d => d.minTemperature),
            yAxisIndex: 1,
          }
        ]
      }
    },
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
      this.getTemperatureHumidityData();
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reset() {
      this.where.recordId = this.data.recordId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.where.sortBy = 'desc';
      this.$nextTick(() => {
        this.reload();
      });
    },

    getTemperatureHumidityData(){
      let params = {
        searchBeginTime : moment().subtract(7, 'days').format('YYYY-MM-DD'),
        searchEndTime : moment().format('YYYY-MM-DD'),
        recordId : this.data.recordId,
        sortBy : "asc"
      };
      if (this.where.searchBeginTime && this.where.searchEndTime) {
        params.searchBeginTime = this.where.searchBeginTime;
        params.searchEndTime = this.where.searchEndTime;
      }

      DeviceSlurryScraperTodayConsumptionApi.findCalenAndComsumptionList(params).then(list => {
        let _temperatureHumidityData = [];
        if (list && list.length > 0) {
          list.forEach(function(item) {
            let formatItem = {
              today : item.today,
              consumptionTotal : (item.consumptionTotal ? (item.consumptionTotal * 0.01).toFixed(2) : 0),
              calenNum : item.calenNum,
              minTemperature : item.minTemperature
            };
            _temperatureHumidityData.push(formatItem);
          });
        } else {
          let last20 = moment().subtract(7, 'days');
          let last10 = moment().subtract(5, 'days');
          let last5 = moment().subtract(1, 'days');
          _temperatureHumidityData.push({
            today : last20.format('YYYYMMDD'),
            consumptionTotal : 0,
            calenNum : 0,
            minTemperature : 0

          });
          _temperatureHumidityData.push({
            today : last10.format('YYYYMMDD'),
            consumptionTotal : 0,
            calenNum : 0,
            minTemperature : 0
          });
          _temperatureHumidityData.push({
            today : last5.format('YYYYMMDD'),
            consumptionTotal : 0,
            calenNum : 0,
            minTemperature : 0
          });
        }
        this.temperatureHumidityData = _temperatureHumidityData;
      });
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceSlurryScraperTodayConsumptionApi.findCalenAndComsumptionPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
    activated() {
      ['temperatureHumidityChart', 'consumptionTemperatureChart'].forEach((name) => {
        this.$refs[name].resize();
      });
    }
  }
};
</script>
